package org.wso2.carbon.identity.samples.entitlement;

import org.wso2.carbon.identity.entitlement.dto.xsd.PolicyDTO;
import org.wso2.carbon.identity.samples.entitlement.admin.EntitlementPolicyAdminServiceStub;
import org.wso2.carbon.identity.samples.entitlement.admin.Exception;

import java.io.*;
import java.rmi.RemoteException;

/**
 * Created by IntelliJ IDEA.
 * User: pushpalanka
 * Date: 3/24/11
 * Time: 11:00 AM
 * To change this template use File | Settings | File Templates.
 */
public class EntitlementAdminServiceUtilities {

    //Read a policy in a pointed out path
    protected String takePolicy(){



        FileInputStream fstream = null;
        String strLine = " ";
        String fileString= " ";
        try {


            fstream = new FileInputStream("PolicySample.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br1 = new BufferedReader(new InputStreamReader(in));




            //Read File Line By Line

            while ((strLine = br1.readLine()) != null)   {
                fileString = fileString + strLine;

            }
            //Close the input stream
            in.close();
            // Print the content on the console
            System.out.println (fileString);


        }catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }finally {
            try {
                if (fstream != null) {
                    fstream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return fileString;
    }







    protected void adminAddPolicy(String policyString,EntitlementPolicyAdminServiceStub oauth){


        PolicyDTO policyDTO = new PolicyDTO();
        policyDTO.setPolicy(policyString.trim());
        policyDTO.setPolicyId("urn:sample:xacml:2.0:samplepolicy-02") ;

        try{
            oauth.addPolicy(policyDTO);
            System.out.println("Policy added.");
        }
        catch (RemoteException re){
             re.printStackTrace();
        }
        catch(org.wso2.carbon.identity.samples.entitlement.admin.Exception ae){
            ae.printStackTrace();

        }

    }

    protected void adminReadExistingPolicy(EntitlementPolicyAdminServiceStub oauth){

        String policy = null;
        PolicyDTO policyDTO = new PolicyDTO();
        try{

            policyDTO = oauth.getPolicy("urn:sample:xacml:2.0:samplepolicy");
        }
        catch(RemoteException re){
            re.printStackTrace();
        }
        catch(org.wso2.carbon.identity.samples.entitlement.admin.Exception ae){
            ae.printStackTrace();

        }
        policy = policyDTO.getPolicy();
        System.out.println("Read Policy:"+ policy);


    }

    protected void adminRemovePolicy(EntitlementPolicyAdminServiceStub oauth){
        PolicyDTO policyDTO = new PolicyDTO();
        policyDTO.setPolicyId("urn:sample:xacml:2.0:samplepolicy-02");

        try{
            oauth.removePolicy(policyDTO);
            System.out.println("Policy removed.");
        }
        catch(RemoteException re){
            re.printStackTrace();
        }
        catch(org.wso2.carbon.identity.samples.entitlement.admin.Exception ae){
            ae.printStackTrace();
        }

    }
}
